package test2;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class main {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		ArrayList<ArrayList<String>> cables = new ArrayList<ArrayList<String>>();
		
		
		Document doc = Jsoup.connect("http://www.cablewakeboard.net/en/cablesearch.html?action=cablesearch&regionID=-1&countryID=-1").get();
		
		String title = doc.title();
		System.out.println(title);

		Elements divs = doc.getElementsByAttributeValue("style", "margin-bottom:5px;margin-top:10px;");
		for(Element div : divs.select("div")){
//			System.out.println("------------");
			ArrayList<String> cableData = new ArrayList<String>();
			
			Elements table = div.select("table").select("tr").select("td");
			
			String country = table.first().children().attr("alt");
//			System.out.println(country);
			
			String location = table.get(1).select("strong").text();
//			System.out.println(location);
			
			String addInfo = table.get(2).select("a").attr("abs:href");
//			System.out.println(addInfo);
			
			cables.add(cableData);
			
			Document doc2 = Jsoup.connect(addInfo).get();
			
			Elements divs2 = doc2.getElementsByAttributeValue("style", "width: 480px; background-color: #EEEEEE;border:1px dotted #000000;padding:5px;text-align:left;");
			Elements trs = divs2.select("tr");
			String add1 = "";
			String add2 = "";
			String add3 = "";
			
			String phone = "";
			String fax = "";
			String email = "";
			String url = "";
			
			int stringCount = 0;
			for(Element tr : trs){
				if(!tr.select("img").isEmpty()) stringCount = 3;
				switch (stringCount) {
				case 0: 
					add1 = tr.text();
					break;
				case 1: 
					add2 = tr.text();
					break;
				case 2: 
					add3 = tr.text();
					break;
				default:
					break;
				}
				stringCount++;
				
				if(!tr.children().isEmpty()){
					if(tr.child(0).text().contains("Phone:")) phone = tr.child(1).text();
					if(tr.child(0).text().contains("Fax:")) fax = tr.child(1).text();
					if(tr.child(0).text().contains("Email:")) email = tr.child(1).text();
					if(tr.child(0).text().contains("URL:")) url = tr.child(1).text();
				}
			}
			
			cableData.add(country); //0
			cableData.add(location);//1
			cableData.add(add1);//2
			cableData.add(add2);//3
			cableData.add(add3);//4
			cableData.add(addInfo);//5
			cableData.add(phone);//6
			cableData.add(fax);//7
			cableData.add(email);//8
			cableData.add(url);//9
			
//			System.out.println(add1+" - "+add2+" - "+add3+" phone: "+phone+" fax: "+fax+" email: "+email+" url: "+url);
		}
	}
}
